// mircoApp/new-auction/components/optionPopup/optionPopup.js
const app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    showPopup: {
      type: Boolean,
      value: false
    },
    pageName: {
      type: String,
      value: ''
    },
    isJoin: {
      type: Boolean,
      value: false
    },
    isPick: {
      type: Boolean,
      value: false
    },
    item: {
      type: null
    },
    isPick_new: {
      type: Boolean,
      default: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    value: 0
  },
  observers: {
    'showPopup,isPick,isJoin,isPick_new': function (showPopup, pick, join, isPick_new) {

      if (pick) {
        if (showPopup) {
          this.setData({
            value: 1
          })
        }
      } else {
        if (join) {
          this.setData({
            value: this.data.item.min_count
          })
        } else {
          if (isPick_new) {
            if (showPopup) {
              this.setData({
                value: 1
              })
            }
          } else {
            this.setData({
              value: this.data.item.usable_auction_goods_count
            })
          }
        }
      }
    }
  },
  lifetimes: {
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en,
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    stepperChange(e) {
      this.data.value = e.detail;
      this.setData({
        value: e.detail
      })
    },
    closeBtn() {
      this.triggerEvent('closePopup')
    },
    allTap() {
      if (this.data.isPick_new) {
        this.setData({
          value: this.data.item.stock
        })
        return
      }
      if (this.data.isPick) {
        this.setData({
          value: this.data.item.usable_count
        })
      } else if (this.data.isJoin) {
        this.setData({
          value: this.data.item.max_auction_num
        })
      } else {
        this.setData({
          value: this.data.item.usable_auction_goods_count
        })
      }
    },
    pickUp() {
      if (this.data.isPick_new) {
        console.log(this.data.value, 'value')
        this.triggerEvent('pickUp_new', {
          id: this.data.item.activity_id,
          value: this.data.value
        })
        return
      }
      this.triggerEvent('pickUp', {
        id: this.data.item.id,
        value: this.data.value
      })
    },
    async joinTap() {
      if (!this.data.value) return app.tips('参拍数量不能为空');
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.new-auction.frontend.auction.auction"),
        data: {
          id: this.data.item.id,
          num: this.data.value
        }
      })
      app.tips(data.msg)
      if (data.result) {
        this.closeBtn();
        this.triggerEvent("onUpdate")
      }
    },
    async entrust() {
      wx.showModal({
        content: `是否确认委拍${this.data.item.has_one_goods.title}${this.data.value}个`,
        showCancel: false,
        complete: async (res) => {
          if (res.confirm) {
            let json = {
              activity_id: this.data.item.id,
              num: this.data.value
            };
            let {
              data
            } = await app._postNetWork({
              url: app.getNetAddresss("plugin.new-auction.frontend.entrust.entrust"),
              data: {
                id: this.data.item.id,
                num: this.data.value
              }
            })
            app.tips(data.msg)
            if (data.result) {
              this.closeBtn();
              this.triggerEvent("onUpdate")
            }
          }
        }
      })

    }
  }
})